package dev.spb3.mapper;

import dev.spb3.model.Order;
import dev.spb3.model.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface OrderMapper {

    @Select("select * from orders where id = #{orderId}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "orderDate", column = "order_date"),
            @Result(property = "amount", column = "amount"),
            @Result(property = "user", column = "user_id", one = @One(select = "getUserById"))
    })
    Order getOrderWithNestedUserQuery(int orderId);
    @Select("select * from users where id = #{userId}")
    User getUserById(int userId);

    @Select("select o.*, u.id as user_id, u.username, u.email, u.registered_date "
    + "from orders o "
    + "left join users u on o.user_id = u.id "
    + "where o.id = #{orderId}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "orderDate", column = "order_date"),
            @Result(property = "amount", column = "amount"),
            @Result(property = "user.id", column = "user_id"),
            @Result(property = "user.username", column = "username"),
            @Result(property = "user.email", column = "email"),
            @Result(property = "user.registeredDate", column = "registered_date")
    })
    Order getOrderWithUser(int orderId);
}
